<?php
// +----------------------------------------------------------------------
// | Azthink通用开发平台 [ TOMORROW IS ANOTHER DAY ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018-2019 http://www.azthink.top All rights reserved.
// +----------------------------------------------------------------------
// | Author {__AUTHOR_DESCRIPTION__}
// +----------------------------------------------------------------------
namespace app\{__MODULE_NAME__}\controller;

use app\{__MODULE_NAME__}\model\{__CONTROLLER_NAME__} as {__CONTROLLER_NAME__}Model;
use app\admin\controller\Admin;
/**
 * {__CONTROLLER_DESCRIPTION__}控制器
 * 
 * @author {__AUTHOR_DESCRIPTION__}
 * @createAt {__CREATE_TIME__}

 *        
 */
class {__CONTROLLER_NAME__} extends Admin {
	
	/**
	 * 列表页面
	 */
	public function index($title = null, $status = null) {
	 	cookie('__forward__', $this->request->url());

		// 数据列表
		$data_list = {__CONTROLLER_NAME__}Model::lists($title,$status);
		$this->assign('data_list', $data_list);
		$this->assign('pager', $data_list->render());

		$this->assign('data_status', config('data_status'));
		$this->assign('status', $status);
		$this->assign('title', $title);
		$this->setMeta ('{__CONTROLLER_DESCRIPTION__}管理');
		return $this->fetch ();
	}
	// 新增{__CONTROLLER_DESCRIPTION__}
	public function add() {
		if ($this->request->isPost()) {
			// 表单数据
			$data = $this->request->post();
			// 验证
			$result = $this->validate($data, '{__CONTROLLER_NAME__}');
			if (true !== $result) {
				$this->error($result);
			}
			if ($data = {__CONTROLLER_NAME__}Model::create($data)) {
				$this->log($data['id'], '新增{__CONTROLLER_DESCRIPTION__}成功');
				$this->success('新增{__CONTROLLER_DESCRIPTION__}成功', cookie('__forward__'));
			} else {
				$this->error('新增{__CONTROLLER_DESCRIPTION__}失败');
			}
		} else {
			$this->assign('info', null);
			$this->setMeta('新增{__CONTROLLER_DESCRIPTION__}');
			return $this->fetch();
		}
	}

	// 更新{__CONTROLLER_DESCRIPTION__}
	public function edit($id = null) {
		if ($this->request->isPost()) {
			// 表单数据
			$data = $this->request->post();
			// 验证
			$result = $this->validate($data, '{__CONTROLLER_NAME__}');
			if (true !== $result) {
				$this->error($result);
			}
			if ($data = {__CONTROLLER_NAME__}Model::update($data)) {

				$this->log($data['id'], '更新{__CONTROLLER_DESCRIPTION__}成功');
				$this->success('更新{__CONTROLLER_DESCRIPTION__}成功', cookie('__forward__'));
			} else {
				$this->error('更新{__CONTROLLER_DESCRIPTION__}失败');
			}

		} else {
			$info = {__CONTROLLER_NAME__}Model::get($id);

			$this->assign('info', $info);
			$this->setMeta('更新{__CONTROLLER_DESCRIPTION__}');
			return $this->fetch('add');
		}
	}

    /**
    * 禁用
    * @param int $id
    */
	public function forbid($id){
		if ({__CONTROLLER_NAME__}Model::state($id, -1)){
			$this->log($id, '禁用{__CONTROLLER_DESCRIPTION__}成功');
			$this->success('禁用{__CONTROLLER_DESCRIPTION__}成功');
		}else{
			$this->error('禁用{__CONTROLLER_DESCRIPTION__}失败');
		}
	}

    /**
    * 恢复
    * @param int $id
    */
	public function resume($id){
		if ({__CONTROLLER_NAME__}Model::state($id, -1)){
			$this->log($id, '启用{__CONTROLLER_DESCRIPTION__}成功');
		$this->success('启用{__CONTROLLER_DESCRIPTION__}成功');
		}else{
			$this->error('启用{__CONTROLLER_DESCRIPTION__}失败');
		}
	}

    /**
    * 删除
    * @param int $id
    */
	public function delete($id){
		if ({__CONTROLLER_NAME__}Model::state($id, -1)){
			$this->log($id, '删除{__CONTROLLER_DESCRIPTION__}成功');
		$this->success('删除{__CONTROLLER_DESCRIPTION__}成功');
		}else{
			$this->error('删除{__CONTROLLER_DESCRIPTION__}失败');
		}
	}

}